package com.yylending.las.common.widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.yylending.las.R;

/**
 *
 * 沿Y轴循环翻转动画
 * Created by ying_he on 2017/6/27.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class RotationView extends AppCompatImageView {

    ObjectAnimator mLoopReversalAnim;
    private int mDuration;
    private int mDegree;
    private String mPropertyType = "rotationY";

    public RotationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RotationView);
        mDuration = typedArray.getInteger(R.styleable.RotationView_duration, 3000);
        mDegree = typedArray.getInteger(R.styleable.RotationView_degree, 1800);
        mPropertyType = typedArray.getString(R.styleable.RotationView_propertyType);
        typedArray.recycle();

        mLoopReversalAnim = ObjectAnimator.ofFloat(this, mPropertyType, 0, mDegree);
        mLoopReversalAnim.setInterpolator(new LinearInterpolator());
        mLoopReversalAnim.setDuration(mDuration);
        mLoopReversalAnim.setInterpolator(new DecelerateInterpolator());
        mLoopReversalAnim.setRepeatCount(ValueAnimator.INFINITE);
        mLoopReversalAnim.setRepeatMode(ValueAnimator.RESTART);
    }

    public void startRotation(){
        mLoopReversalAnim.start();
    }

    public void endRotation(){
        mLoopReversalAnim.end();
    }

    public void cancelRotation(){
        mLoopReversalAnim.cancel();
    }
}

